﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace DevEx.Thanh.UserControl
{
    public partial class ucCommands : DevExpress.XtraEditors.XtraUserControl
    {
        private ControlCollection setCtrl =null;
        public ucCommands()
        {
            InitializeComponent();
        }
        public ucCommands(ControlCollection  _ctrls)
        {
            InitializeComponent();
            setCtrl = _ctrls;
        }
        public void setControls(ControlCollection _ctrls)
        {
            setCtrl = _ctrls;
        }
        public EventHandler btAdd_ClickEvent;
        private void btAdd_Click(object sender, EventArgs e)
        {
            EnableButton(false);
            EnableControl(true, setCtrl );
            ResetControl(setCtrl);
            if (btAdd_ClickEvent != null)
                btAdd_ClickEvent (this,e);
        }

        public EventHandler btDel_ClickEvent;   
        private void btDel_Click(object sender, EventArgs e)
        {
            if (btDel_ClickEvent != null)
                btDel_ClickEvent(this, e);
        }
        public EventHandler btEdit_ClickEvent;
        private void btEdit_Click(object sender, EventArgs e)
        {
            EnableButton(false);
            EnableControl(true, setCtrl);
            if (btEdit_ClickEvent != null)
                btEdit_ClickEvent(this, e);
        }
        public EventHandler btSave_ClickEvent;
        private void btSave_Click(object sender, EventArgs e)
        {
            if (btSave_ClickEvent != null)
                btSave_ClickEvent(this, e);
        }
        public EventHandler btHuy_ClickEvent;
        private void btHuy_Click(object sender, EventArgs e)
        {
            EnableButton(true);
            EnableControl(false, setCtrl);
            if (btHuy_ClickEvent != null)
                btHuy_ClickEvent(this, e);
        }
        public void EnableButton(Boolean val)
        {
            btAdd.Enabled = val;
            btDel.Enabled = val;
            btEdit.Enabled = val;

            btSave.Enabled = !val;
            btHuy.Enabled = !val;
        }
        public void EnableControl(Boolean val, ControlCollection ctrls)
        {
            foreach (Control c in ctrls )
            {
                EnableControl(val, c.Controls);
                if (c.GetType() == typeof(TextEdit ))
                {
                    TextEdit t = (TextEdit)c;
                    t.Enabled = val;
                }
                else if (c.GetType() == typeof(LookUpEdit))
                {
                    LookUpEdit l = (LookUpEdit )c;
                    l.Enabled = val;
                }
                else if (c.GetType() == typeof(CheckEdit))
                {
                    CheckEdit k = (CheckEdit )c;
                    c.Enabled = val;
                }
            }
        }
        public void ResetControl(ControlCollection ctrls)
        {
            foreach (Control c in ctrls)
            {
                ResetControl(c.Controls);
                if (c.GetType() == typeof(TextEdit))
                {
                    TextEdit t = (TextEdit)c;
                    t.EditValue ="";
                }
                else if (c.GetType() == typeof(LookUpEdit))
                {
                    LookUpEdit l = (LookUpEdit)c;
                    l.ItemIndex = -1;
                }
            }
        }
    }
}
